package mspbots.qbd.common;

import com.fasterxml.jackson.databind.JsonNode;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * mspbots.data.autotask.response.ResultResponse
 *
 * @author <a href="https://github.com/vnobo">Alex bob</a>
 * @date Created by 2020/7/22
 */
@Data
@NoArgsConstructor
public class ResultSync implements Serializable {

    @Min(1)
    private long tenantId;

    @NotEmpty
    private UUID requestId;

    @NotEmpty
    private JsonNode params;

    @NotEmpty
    private LocalDateTime time;

    private long elapsedTime;
    private int count;
    private HttpStatus status;

    @NotEmpty
    private String action;

    @NotEmpty
    private String type;

    @NotEmpty
    private Object msg;

    @Builder
    public ResultSync(long tenantId, int count, long elapsedTime, HttpStatus status, String action, Object msg) {
        this.count = count;
        this.elapsedTime = elapsedTime;
        this.msg = msg;
        this.status = status;
        this.action = action;
        this.requestId = UUID.randomUUID();
        this.time = LocalDateTime.now();
        this.type = "desktop";
        this.tenantId = tenantId;
    }

    /**
     * @return default ResultSync body
     */
    public static ResultSync withDefault(String action) {
        return new ResultSync(0, 0, System.currentTimeMillis(), HttpStatus.OK, action,
                "This request api success.");
    }

    public ResultSync status(HttpStatus status) {
        this.setStatus(status);
        return this;
    }

    public ResultSync tenantId(long tenantId) {
        this.setTenantId(tenantId);
        return this;
    }

}
